'use strict';

// 超出省略对象
function Ellipsis() {

}

// 类名操作................................................................................................

// 查找类名元素
function getElementsByClass(node, className) {
	if (node.getElementsByClassName) {
		return node.getElementsByClassName(className);
	} else {
		var results = new Array(),
			elements = node.getElementsByTagName('*');
		forEach(elements, function (element, index) {
			if (element.className.indexOf(className) != -1) {
				results[results.length] = element;
			}
		});
	}
}

// 类名新增
function addClass(element, className) {
	if (element.classList) {
		element.classList.add(className);
	} else {
		element.className += ' ' + className;
	}

	return element;
}

// 类名删除
function removeClass(element, className) {
	if (element.classList) {
		element.classList.remove(className);
	} else {
		element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
	}

	return element;
}

// 类名包含
function hasClass(element, className) {
	if (element.classList) {
		return element.classList.contains(className);
	} else {
		return new RegExp('(^| )' + className + '( |$)', 'gi').test(element.className);
	}
}

// 数组操作.........................................................................................................

// 遍历数组
function forEach(elements, callback) {
	if ([].forEach) {
		[].forEach.call(elements, callback);
	} else {
		for (var i = 0; i < elements.length; i++) {
			callback(elements[i], i);
		}
	}
}

// 兄弟元素..........................................................................................................

// 所有其他兄弟元素数组，以及当前索引
function siblings(obj) {
	var self = obj,
		arr = [],
		index,
		p = self.parentNode.children;

	for (var i = 0, pl = p.length; i < pl; i++) {
		if (p[i] !== self) {
			arr.push(p[i]);
		} else {
			index = i;
		}
	}

	return {
		arr: arr,
		index: index
	};
}

// 遍历至指定父元素
function findParent(obj, target) {
	while (obj !== target) {

		if (obj == null) {
			return false;
		}

		obj = obj.parentNode;
	}

	return obj;
}

//ele是内部元素，target是你想找到的包裹元素
function getTargetNode(ele, target) {
	if (!ele || ele === document) return false;
	return ele === target ? true : getTargetNode(ele.parentNode, target);
}

// Tab切换
function tab(element, className) {
	// 自身添加类名
	addClass(element, className);
	// 兄弟元素移除类名
	siblings(element).arr.forEach(function (sibling) {
		removeClass(sibling, className);
	});
}

// 事件操作..........................................................................................................

// 绑定事件
function addEvent(element, type, handle, capture) {
	if (element.addEventListener) {
		element.addEventListener(type, handle, capture);
	} else if (element.attachEvent) {
		element.attachEvent('on' + type, handle);
	} else {
		element['on' + type] = handle;
	}
}

// 移除事件
function removeEvent(element, type, handle, capture) {
	if (element.removeEventListener) {
		element.removeEventListener(element, type, handle, capture);
	} else if (element.detachEvent) {
		element.detachEvent('on' + type, handle);
	} else {
		element['on' + type] = null;
	}
}

// 事件代理
function eventBroker(event, className, fn) {
	var event = event || window.event,
		target = event.target;

	while (target) {
		if (target && target.nodeName == '#document') {
			break;
		} else if (hasClass(target, className)) {
			fn();
			break;
		}

		target = target.parentNode;
	}
}

// 触发事件
function trigger(element, type) {
	if (document.createEvent) {
		var event = document.createEvent('HTMLEvents');
		event.initEvent(type, true, false);
		element.dispatchEvent(event);
	} else {
		element.fireEvent('on' + type);
	}
}

// 获取图片的原始尺寸.........................................................................................................

function getImgNaturalDimensions(img) {
	var nWidth, nHeight;

	if (img.naturalWidth) { // 现代浏览器
		nWidth = img.naturalWidth;
		nHeight = img.naturalHeight;
	} else { // IE6/7/8
		var imgae = new Image();

		image.src = img.src;
		image.onload = function () {
			nWidth = image.width;
			nHeight = image.height;

			return [nWidth, nHeight];
		}
	}

	return [nWidth, nHeight];
}

function formatNum(str) {
	var newStr = "",
		count = 0;

	// 非小数的情况
	if (str.indexOf(".") == -1) {

		for (var i = str.length - 1; i >= 0; i--) {
			// 第4,7,10位的情况
			if (count % 3 == 0 && count != 0) {
				newStr = str.charAt(i) + "," + newStr;
			} else {
				newStr = str.charAt(i) + newStr;
			}

			count++;
		}
		//自动补齐两位小数
		// str = newStr + ".00";
		return newStr;
	} else {
		for (var i = str.indexOf(".") - 1; i > 0; i--) {
			if (count % 3 == 0 && count != 0) {
				newStr = str.charAt(i) + "," + newStr;
			} else {
				newStr = str.charAt(i) + newStr;
			}

			count++;
		}

		str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);
	}
}


// Scroll事件................................................................................

// 返回顶部
function topBack() {
	var timer = null;

	timer = setInterval(function () {
		var backTop = document.body.scrollTop;
		var speed = backTop / 5;
		document.body.scrollTop = backTop - speed;

		if (backTop == 0) {
			clearInterval(timer);
		}
	}, 30);
}

// 获取原始数据
Ellipsis.prototype.getOriginalContent = function (obj) {
	if (!obj) return;
	obj.originalContent = obj.innerHTML;
};

// 裁剪数据，添加省略号
Ellipsis.prototype.ellipsis = function (obj, max) {
	if (!obj) return;
	// 去除超出部分
	obj.innerHTML = obj.originalContent.substr(0, max);
	// 默认情况
	obj.innerHTML += '...';
};

// 裁剪数据，添加省略号
Ellipsis.prototype.ellipsisNormal = function (obj, max, type) {
	if (!obj) return;
	if (obj.originalContent.length > max) {
		// 去除超出部分
		obj.innerHTML = obj.originalContent.substr(0, max);
		// 默认情况
		obj.innerHTML += '...';

		var self = this;
		var aNode = document.createElement('a');

		aNode.href = 'javascript:;';
		aNode.innerHTML = type[0];

		aNode.onclick = function (e) {

			self.recoverNormal(obj, max, type);

			var parentLi = obj.parentNode;
			addClass(parentLi, 'active');
		};
		obj.appendChild(aNode);
	}
};

// 恢复原始数据
Ellipsis.prototype.recover = function (obj, max) {
	if (!obj) return;
	// 清空定时器
	if (obj.interval) {
		this.ellipsis(obj, max);
		clearInterval(obj.interval);
	}

	if (!max) {
		obj.innerHTML = obj.originalContent; // 直接恢复原始数据
	} else {
		// 去除省略号
		obj.innerHTML = obj.originalContent.substr(0, max);
		// 设置段落当前文字总长度
		obj.currentLength = max;
		// 添加定时器，增加可显示字数
		obj.interval = setInterval(function () {
			// 可显示字数++
			obj.currentLength++;
			// 更新段落
			obj.innerHTML = obj.originalContent.substr(0, obj.currentLength);

			// 当段落恢复至原始内容时，关闭定时器
			if (obj.currentLength === obj.originalContent.length) {
				obj.currentLength = max;
				clearInterval(obj.interval);
			}
		}, 1500 / (obj.originalContent.length - max));
	}
};

// 恢复原始数据--普通
Ellipsis.prototype.recoverNormal = function (obj, max, type) {
	if (!obj) return;
	obj.innerHTML = obj.originalContent;

	var self = this;
	var aNode = document.createElement('a');

	aNode.href = 'javascript:;';
	aNode.innerHTML = type[1];
	addClass(aNode, 'up')
	aNode.onclick = function (e) {
		self.ellipsisNormal(obj, max, type);

		var parentLi = obj.parentNode;
		removeClass(parentLi, 'active');
	};

	obj.appendChild(aNode);
};